# coding=utf-8

import tornado.web

from CommonLib import *
from WrapCursor import WrapCursor
from database.models.Controller import Controller
from database.models.DatabaseCommon import *


class NewDeviceHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor()
        try:
            j = json.loads(self.request.body)
            ojson = j
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            self.write(r.Error(u'错误:' + u'请求数据错误'))
            return

        session1 = sessionmaker(bind=engine, autoflush=True)
        session = session1()
        ctlSet = session.query(Controller).filter_by(Name='').all()
        jsonret = {'Code': 0, 'desc': 'success'}
        data = []
        for i in ctlSet:
            o = dict()
            o['id'] = i.id
            o['HardID'] = i.HardID
            o['Type'] = i.TypeID
            o['Value'] = i.Value
            o['Name'] = ''
            data.append(o)
        jsonret['data'] = data
        self.write(json.dumps(jsonret))
